package com.tianqu.tms.www.settle.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.TransferSettlementDate;
import com.tianqu.tms.entity.TransferSettlementDay;
import com.tianqu.tms.entity.UserAuth;
import com.tianqu.tms.www.settle.actionform.TransferSettleActionForm;
import com.tianqu.tms.www.settle.viewhelper.TransferSettleViewHelper;

public class TransferSettleSearchDisPatchAction extends TmsAction {

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {

		String auctionForward = "";
		
		TransferSettleActionForm inForm = (TransferSettleActionForm)form;
		TransferSettleViewHelper view = new TransferSettleViewHelper();
		view.setSettleType(inForm.getSettleType());
		view.setStartDate(inForm.getStartDate());
		view.setEndDate(inForm.getEndDate());
		view.setSettleId(inForm.getSettleId());
		TmsUserProfile profile = uc.getUserProfile();
		
		if(inForm.getSearchOnButton()!=null && !"".equals(inForm.getSearchOnButton()))
		{
			auctionForward = "noCheckTop";
		}else if(inForm.getSettleOnButton()!=null && !"".equals(inForm.getSettleOnButton()))
		{
			auctionForward = "settleTop";
		}else{
			if("1".equals(inForm.getSettleType()))
			{
				//当前
				auctionForward = "currentPage";
			}
			if("2".equals(inForm.getSettleType()))
			{
				//下一个
				TransferSettlementDate settleDate =new TransferSettlementDate();
				settleDate = settleDate.getDateInfo(1,profile.getUniqueId());
				if(settleDate!=null)
				{
					view.setStartDate(settleDate.getStartDate());
					view.setEndDate(DateUtil.getCurrentTime1());
					view.setSettleId(String.valueOf(settleDate.getId()));
				}else{
					view.setShowBsFlag("1");
				}
				//添加验证 日期带时分秒验证
				auctionForward = "currentPage";
			}
			if("3".equals(inForm.getSettleType()))
			{
				//查询历史结算信息
				UserAuth auth = new UserAuth();
				boolean flag = auth.getAuthInfoByRole(profile.getUniqueId(), 470);
				if(flag)
				{
					//为470 没有不显示
					TransferSettlementDate date = new TransferSettlementDate();
					List list = date.getSettleDateInfo(3,profile.getUniqueId());
					view.setSettleDateList(list);
					if(list.size()==0)
					{
						view.setShowBsFlag("1");
					}
				}else{
					//不为470
					TransferSettlementDay day = new TransferSettlementDay();
					List list = day.getSettleDayInfo();
					view.setSettleDateList(list);
					if(list.size()==0)
					{
						view.setShowBsFlag("1");
					}
				}
				auctionForward = "currentPage";
			}
		}
		uc.setAttribute(TmsConstants.VIEWHELP, view);
		return mapping.findForward(auctionForward); 
	}
}
